package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Pl;
import service.PlService;

import java.util.List;

/**
 * 评论表控制器
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/pl")
public class PlController {
    @Autowired
    private PlService plService;

    // 根据ID查询评论
    @GetMapping("/{id}")
    public Pl getPlById(@PathVariable Integer id) {
        return plService.findPlById(id);
    }

    // 查询急救知识的评论
    @GetMapping("/jjzs/{jjzsId}")
    public List<Pl> getByJjzsId(@PathVariable Integer jjzsId) {
        return plService.findByJjzsId(jjzsId);
    }

    // 查询文章的评论
    @GetMapping("/wz/{wzId}")
    public List<Pl> getByWzId(@PathVariable Integer wzId) {
        return plService.findByWzId(wzId);
    }

    // 查询用户的评论
    @GetMapping("/user/{yhId}")
    public List<Pl> getByYhId(@PathVariable Integer yhId) {
        return plService.findByYhId(yhId);
    }

    // 新增评论
    @PostMapping("/add")
    public int addPl(@RequestBody Pl pl) {
        // 确保必填字段不为null
        if (pl.getYhId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        if (pl.getContent() == null || pl.getContent().trim().isEmpty()) {
            throw new IllegalArgumentException("评论内容不能为空");
        }
        return plService.add(pl);
    }

    // 更新评论
    @PutMapping("/update")
    public int updatePl(@RequestBody Pl pl) {
        if (pl.getId() == null) {
            throw new IllegalArgumentException("评论ID不能为空");
        }
        return plService.update(pl);
    }

    // 删除评论
    @DeleteMapping("/{id}")
    public int deletePl(@PathVariable Integer id) {
        return plService.deleteById(id);
    }

    // 删除急救知识的评论
    @DeleteMapping("/jjzs/{jjzsId}")
    public int deleteByJjzsId(@PathVariable Integer jjzsId) {
        return plService.deleteByJjzsId(jjzsId);
    }

    // 删除文章的评论
    @DeleteMapping("/wz/{wzId}")
    public int deleteByWzId(@PathVariable Integer wzId) {
        return plService.deleteByWzId(wzId);
    }

    // 删除用户的评论
    @DeleteMapping("/user/{yhId}")
    public int deleteByYhId(@PathVariable Integer yhId) {
        return plService.deleteByYhId(yhId);
    }
}